package com.ross.mode.controller;

import com.alibaba.fastjson.JSON;
import com.ross.entity.card.IQiYiCardService;
import com.ross.entity.coupon.CouponResult;
import com.ross.entity.coupon.CouponService;
import com.ross.entity.goods.DeliverReq;
import com.ross.entity.goods.GoodsService;
import com.ross.mode.entity.AwardReq;
import com.ross.mode.entity.AwardRes;

/**
 * @version 1.0.0
 * @className: AwardController
 * @description: 使用if-else实现功能
 * @author: zhangjunfa
 * @date: 2023/3/26 11:53
 */
public class AwardController {

    public AwardRes awardToUser(AwardReq req) {
        String json = JSON.toJSONString(req);
        AwardRes awardRes = null;
        try {
            System.out.println("奖品发放开始:{},req:{}" + req.getUId() + json);
            if (req.getAwardType() == 1) {
                CouponService couponService = new CouponService();
                CouponResult result = couponService.sendCoupon(req.getUId(), req.getAwardNumber(), req.getBizId());
                if ("0000".equals(result.getCode())) {
                    awardRes = new AwardRes("0000", "发放成功");
                } else {
                    awardRes = new AwardRes("0001", result.getInfo());
                }

            } else if (req.getAwardType() == 2) {
                GoodsService goodsService = new GoodsService();
                DeliverReq deliverReq = new DeliverReq();
                deliverReq.setUserName(queryUserName(req.getUId()));
                deliverReq.setUserPhone(queryUserPhoneNumber(req.getUId()));
                deliverReq.setSku(req.getAwardNumber());
                deliverReq.setOrderId(req.getBizId());
                deliverReq.setConsigneeUserName(req.getExtMap().get("consigneeUserName"));
                deliverReq.setConsigneeUserPhone(req.getExtMap().get("consigneeUserPhone"));
                deliverReq.setConsigneeUserAddress(req.getExtMap().get("consigneeUserAddress"));
                Boolean isSuccess = goodsService.deliverGoods(deliverReq);
                if (isSuccess) {
                    awardRes = new AwardRes("0000", "发放成功");
                } else {
                    awardRes = new AwardRes("0001", "发放失败");
                }
            } else if (req.getAwardType() == 3) {
                String bindMobileNumber = queryUserPhoneNumber(req.getUId());
                IQiYiCardService iQiYiCardService = new IQiYiCardService();
                iQiYiCardService.grantToken(bindMobileNumber, req.getAwardNumber());
                awardRes = new AwardRes("0000", "发放成功");
            }
            System.out.println("奖品发放完成{}。" + req.getUId());

        } catch (Exception e) {
            System.out.println("奖品发送失败：{},{},{}" + req.getUId() + "," + json);
            awardRes = new AwardRes("0001", e.getMessage());
        }
        return awardRes;

    }

    private String queryUserName(String uId) {
        return "花花";
    }

    private String queryUserPhoneNumber(String uId) {
        return "15200101232";
    }

}
